package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author alexis
 * @date 2024-11-03
 */
@Slf4j
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo res = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, res);
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        res.setProjectPlans(list);
        return res;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    private void deleteRedisCatch() {
        redisTemplate.delete(CacheConstants.NURSING_PLAN_ALL);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        // 批量保存护理项目计划关系
        int res = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        deleteRedisCatch();
        return res > 0 ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        if (dto.getProjectPlans() != null) {
            nursingProjectPlanMapper.removeProjectListById(nursingPlan.getId());
            nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        }
        int res = updateById(nursingPlan) ? 1 : 0;
        deleteRedisCatch();
        return res > 0 ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids) {
        nursingProjectPlanMapper.removeProjectListById(ids[0]);
        deleteRedisCatch();
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        boolean res = removeById(id);
        deleteRedisCatch();
        return res ? 1 : 0;
    }

    /**
     * 获取所有护理计划
     *
     * @param wrapper
     * @return
     */
    @Override
    public List<NursingPlan> allList(LambdaQueryWrapper<NursingPlan> wrapper) {
        String allNursingPlan = redisTemplate.opsForValue().get(CacheConstants.NURSING_PLAN_ALL);
        List<NursingPlan> list = JSONUtil.toList(allNursingPlan, NursingPlan.class);
        if (list != null && list.size() > 0) {
            return list;
        }
        list = list(wrapper);
        redisTemplate.opsForValue().set(CacheConstants.NURSING_PLAN_ALL, JSONUtil.toJsonStr(list));
        return list;
    }
}
